package com.zhipu.balance.ui.home.calculate.rice

import android.os.Bundle
import android.text.SpannableString
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import com.zhipu.balance.R
import com.zhipu.balance.databinding.FragmentRiceYellowGrainBinding
import com.zhipu.balance.databinding.FragmentRiceYieldBinding
import com.zhipu.balance.mobile.dao.PersonnelDao
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.ui.BaseFragment
import com.zhipu.balance.ui.vm.home.calculate.rice.RiceYellowGrainViewModel
import com.zhipu.balance.utils.InitializeUtils
import org.angmarch.views.SpinnerTextFormatter

/**
 * 稻谷黄粒米
 */
class RiceYellowGrainFragment(var mRecordRice : RecordRice) : BaseFragment<FragmentRiceYellowGrainBinding>(){
    /**
     * VM
     */
    private var mRiceYellowGrainViewModel: RiceYellowGrainViewModel? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        InitializeUtils.AppComponent()?.doInjectionMain(this)
        mRiceYellowGrainViewModel = ViewModelProvider(this, viewModelFactory)[RiceYellowGrainViewModel::class.java]
    }



    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    override fun initContentView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): Int {
        return R.layout.fragment_rice_yellow_grain
    }


    override fun initView() {
        super.initView()
        binding.viewModel = mRiceYellowGrainViewModel
        mRiceYellowGrainViewModel?.mRecordRice = mRecordRice

    }


    /**
     * 重量
     */
    fun setWeighing(weighing : String?){
        if(null != mRiceYellowGrainViewModel){
            mRiceYellowGrainViewModel?.weighing = weighing
        }

    }


    override fun initData() {
        super.initData()
        /**
         * 检测人员
         */
        var mPersonnelList = PersonnelDao.getInstance().allList
        if(null != mPersonnelList && mPersonnelList.size > 0){
            var mSpinnerTextFormatter = SpinnerTextFormatter<Personnel> { item ->
                mRiceYellowGrainViewModel?.mPersonnel = item
                SpannableString(item?.name)
            }
            binding.NiceSpinner.setSpinnerTextFormatter(mSpinnerTextFormatter)
            binding.NiceSpinner.setSelectedTextFormatter(mSpinnerTextFormatter)
            binding.NiceSpinner.attachDataSource(mPersonnelList)
            binding.NiceSpinner.setOnSpinnerItemSelectedListener { parent, view, position, id ->
                var mPersonnel : Personnel = binding.NiceSpinner.selectedItem as Personnel
                if(null != mPersonnel){
                    mRiceYellowGrainViewModel?.mPersonnel = mPersonnel
                }
            }
        }
        /**
         * 更新页面
         */
        mRiceYellowGrainViewModel?.updateRecordRice()
    }

    /**
     * 更新实体
     */
    fun setRecordRice(mRecordRice : RecordRice){
        mRiceYellowGrainViewModel?.mRecordRice = mRecordRice
    }


    /**
     * 更新稻谷实体
     */
    fun updateRecordRice(mRecordRice : RecordRice){
        mRiceYellowGrainViewModel?.mRecordRice = mRecordRice
        mRiceYellowGrainViewModel?.updateRecordRice()
    }

    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        mRiceYellowGrainViewModel?.releaseRecordRice()
    }



    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算
         */
        mRiceYellowGrainViewModel?.calculation()
    }
}